#include "os_linux.h"
#include "linux_thread.h"
#include "tl_types.h"
#include "tl_packets_api.h"
#include "tl_serial_api.h"
#include "tl_program_api.h"
#include "tl_controlers_api.h"
#include "tl_api.h"
#include "lib_tl_log.h"
#include "lib_tl_config.h"
#include "lib_tl_com.h"
#include "lib_tl_packets.h"
#include "lib_tl_client.h"
#include "sim_tl_main.h"

int sim_tl_get_status(tl_api_status_t *pStatus)
{
	int iFd = sim_get_client_fd();
	if(OK == tl_mgmt_send(iFd, TL_API_GET_STATUS, NULL, 0))
		if(sizeof(tl_api_status_t) == tl_mgmt_get(iFd, (uint8_t*)(pStatus), sizeof(tl_api_status_t)))
			return OK;
	return ERROR;	
}

int sim_tl_get_mod(char *szName, tl_api_status_mod_t *pModStatus)
{
	uint8_t iModNum=0;
	int iFd = sim_get_client_fd();
	
	if(OK == tl_mgmt_send(iFd, TL_API_GET_STATUS_MOD, (uint8_t*)szName,strlen(szName) + 1))
		if(	sizeof(iModNum) == tl_mgmt_get(iFd, (uint8_t*)(&iModNum), sizeof(iModNum) &&
			1 == iModNum))
				if(sizeof(tl_api_status_mod_t) == tl_mgmt_get(iFd, (uint8_t*)(pModStatus), sizeof(tl_api_status_mod_t) ))
					return OK;
	return ERROR;	
}

void sim_tl_mod_state_dump()
{
	uint8_t iModNum, i;
	tl_api_status_mod_t stModStatus;
	int iFd = sim_get_client_fd();	
	
	tl_mgmt_send(sim_get_client_fd(), TL_API_GET_STATUS_MOD, NULL, 0);
	if(sizeof(iModNum) == tl_mgmt_get(iFd, (uint8_t*)(&iModNum), sizeof(iModNum) ))
	{
		for(i=0; i<iModNum; i++)
			if(sizeof(stModStatus) == 
					tl_mgmt_get(iFd, (uint8_t*)(&stModStatus), sizeof(stModStatus) ))
				{	
					tl_log_info(" %s %s", stModStatus.szName, tl_status2string(stModStatus.iStatus));
				}
	}
}

int sim_tl_check_status(uint8_t iDevError, uint8_t iCritical, 
						uint8_t iWarning, tl_program_state_t iProgState)
{
	tl_api_status_t stStatus;
	
	if(OK == sim_tl_get_status(&stStatus))
	{
			if(iDevError != stStatus.iDeviceError)
			{
				tl_log_info("Dev error %d: %d %d %d", iDevError, 
						stStatus.iDeviceError, stStatus.iCritError, stStatus.iWarning);
				sim_tl_mod_state_dump();
				return ERROR;
			}
			if(iCritical != stStatus.iCritError)
			{
				tl_log_info("Crit error %d: %d %d %d", iCritical, 
						stStatus.iDeviceError, stStatus.iCritError, stStatus.iWarning);
				sim_tl_mod_state_dump();		
				return ERROR;
			}
			if(iWarning != stStatus.iWarning)
			{
				tl_log_info("Warning %d: %d %d %d", iWarning, 
							stStatus.iDeviceError, stStatus.iCritError, stStatus.iWarning);
				sim_tl_mod_state_dump();			
				return ERROR;
			}
			if(iProgState != stStatus.iProgState) 
			{
				tl_log_info("Program %d != %d", iProgState, stStatus.iProgState);						
				return ERROR;		
			}
		return OK;		
	}
	return ERROR;
}


